package com.bondex.charter.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 数据库相关链接
 */
@Configuration
public class JdbcTemplateConfig {

	//region 主数据库 - mybatis
	@Primary
	@Bean(name = "primaryDataSourceProperties")
	@Qualifier("primaryDataSourceProperties")
	@ConfigurationProperties(prefix = "spring.datasource")
	public DataSourceProperties primaryDataSourceProperties() {
		return new DataSourceProperties();
	}

	@Primary
	@Bean(name = "primaryDataSource")
	@Qualifier("primaryDataSource")
	public DataSource loadPrimaryDataBaseInfo() {
		return primaryDataSourceProperties().initializeDataSourceBuilder().build();
	}

	@Bean(name = "primaryJdbcTemplate")
	public JdbcTemplate getPrimaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) {
		return new JdbcTemplate(dataSource);
	}

	//endregion

}
